package com.yoda.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoda.entity.SysConfig;
import com.yoda.mapper.SysConfigMapper;
import com.yoda.result.Result;
import com.yoda.service.ISysConfigService;
import com.yoda.vo.AllDataVo;
import com.yoda.vo.AllVo;
import com.yoda.vo.ChildrenVo;
import com.yoda.vo.ConfigListVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-05-15
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

    /**
     * 主页配置
     *
     * @return
     */
    @Override
    public Result getConfig() {
        LambdaQueryWrapper<SysConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(SysConfig::getAttrKey,"login_title","login_bg"
        ,"system_name","system_logo","web_ico");
        queryWrapper.select(SysConfig::getAttrValue);
        List<Object> list = listObjs(queryWrapper);
        Map map = new HashMap();
        map.put("login_title",list.get(0));
        map.put("login_bg",list.get(1));
        map.put("system_name",list.get(2));
        map.put("system_logo",list.get(3));
        map.put("web_ico",list.get(4));

        return Result.success(map);
    }

    /**
     * 查询站点配置信息
     *
     * @return
     */
    @Override
    public Result all() {
        AllVo vo = new AllVo();
        //定义list数据封装容器

        //定义alldata数据封装容器
        List<SysConfig> sysConfigList = list(null);
        vo.setAllData(sysConfigList);

        Map configForm = new HashMap();
        Map configRules = new HashMap();
        sysConfigList.stream().forEach(sysConfig->{
            String attrKey = sysConfig.getAttrKey();
            String attrValue = sysConfig.getAttrValue();
            if (!attrKey.equals("site_name")){
                configForm.put(attrKey,attrValue);
            }else {
                configRules.put(attrKey,sysConfig.getValidator());
            }
        });
        vo.setConfigForm(configForm);
        vo.setConfigRules(configRules);

        QueryWrapper<SysConfig> qw = new QueryWrapper<SysConfig>();
        qw.select("tab_value");
        qw.groupBy("tab_value");
        List<Map<String, Object>> maps = listMaps(qw);
        List<ConfigListVo> configListVoList = new ArrayList<>();
        maps.stream().forEach(map->{
            ConfigListVo configListVo = new ConfigListVo();
            String value = (String) map.get("tab_value");
            configListVo.setKey(value);
            if (value.equals("base")){
                configListVo.setName("基础设置");
            }else if (value.equals("custom")){
                configListVo.setName("其他设置");
            }else {
                configListVo.setName("系统设置");
            }

            QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<SysConfig>();
            queryWrapper.select("group_value");
            queryWrapper.eq("tab_value",value);
            queryWrapper.groupBy("group_value");
            List<Map<String, Object>> mapList = listMaps(queryWrapper);

            List<ChildrenVo> children = new ArrayList<>();
             mapList.stream().forEach(map1->{
                ChildrenVo childrenVo = new ChildrenVo();
                String name = (String) map1.get("group_value");

                LambdaQueryWrapper<SysConfig> query = Wrappers.lambdaQuery();
                query.eq(SysConfig::getGroupValue,name);

                List<SysConfig> sysConfigs = list(query);


                childrenVo.setData(sysConfigs);
                childrenVo.setName(name);
                children.add(childrenVo);
                configListVo.setChildren(children);
            });

            configListVoList.add(configListVo);
        });

        vo.setList(configListVoList);



        return Result.success(vo);
    }

}
